package com.aaa.sbr.controller;

import com.aaa.sbr.entity.Greeting;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @ fileName:GreetingController
 * @ description:
 * @ author:zhz
 * @ createTime:2023/7/17 16:57
 * @ version:1.0.0
 */
@RestController
//@RequestMapping("greeting") //父路径映射
public class GreetingController {

    //定义模板字符串  是static 静态的  final 最终的   在其他地方不能改变
    private static  final String template = "Hello, %s!";
    //实例化一个原子计数器  可以在多线程环境中，保证原子性
    private    final AtomicLong counter = new AtomicLong();

    /**
     * 欢迎方法
     * @param realName
     * @return
     */
    @GetMapping("greeting")
    public Greeting greeting(@RequestParam(value = "name",defaultValue = "World")String realName){
       /* int counter = 0;
        counter++;*/
        //counter.incrementAndGet
        return new Greeting(counter.incrementAndGet(),String.format(template,realName));
        //  return new Greeting(counter.incrementAndGet(),"hello "+realName+"!");
    }
}
